package com.hlx.controller.admin;

import com.hlx.entity.admin.Administrator;
import com.hlx.service.admin.AdministratorService;
import com.hlx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/administrator")
public class AdministratorController {
    @Autowired
    private AdministratorService administratorService;

    @PostMapping("/login")
    public Result<Administrator> Login(@RequestBody Administrator admin){
        log.info("{}",admin);
        if(administratorService.login(admin) == null){
            return Result.error("账户或密码错误");
        }
        return Result.success(administratorService.login(admin));
    }

    @PutMapping ("/updateAdmin")
    public Result<String> updateAdmin(@RequestBody Administrator admin){
        log.info("{}",admin);
        if(admin.getAdministratorPassword() != null){
            admin.setAdministratorPassword(DigestUtils.md5DigestAsHex(admin.getAdministratorPassword().getBytes()));
        }
        if(!administratorService.updateById(admin)){
            return Result.error("修改失败，请稍后重试");
        }
        return Result.success("修改成功");
    }
}
